<template>
  <div>

    <el-dialog title="用户信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input disabled v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input disabled v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth">
          <el-select v-model="form.roleId" placeholder="请选择">
            <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth">
          <el-select v-model="form.deptId" placeholder="请选择">
            <el-option v-for="item in deptOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveUserEnter">确 定</el-button>
      </div>
    </el-dialog>

    <el-row :gutter="20">
      <el-col :span="8">
        <div class="grid-content bg-purple">姓名:<el-input v-model="user.name" placeholder="请输入内容" style="width:70%">
          </el-input>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-purple">地址:<el-input v-model="user.address" placeholder="请输入内容" style="width:70%">
          </el-input>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-purple">
          <el-button @click="listUser()">检索</el-button>
        </div>
      </el-col>
    </el-row>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="name" label="姓名" width="100">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="120">
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="50">
      </el-table-column>
      <el-table-column prop="address" label="地址">
      </el-table-column>
      <el-table-column prop="deptName" label="部门名称">
      </el-table-column>
      <el-table-column prop="roleName" label="角色">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button @click="editUser(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
          <el-button @click="resetPass(scope.row)" type="text" size="small">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @current-change="listUser1" background layout="prev, pager, next" :total="total">
    </el-pagination>
  </div>
</template>
  
<script>
export default {
  name: "User",
  created() {
    this.listUser()
  },
  components: {},
  data() {
    return {
      tableData: [],
      current: 1,
      total: 1,
      user: {
        name: null,
        address: null
      },
      dialogFormVisible: false,
      form: {},
      roleOptions: [],
      deptOptions: [],
      formLabelWidth: '120px'
    };
  },
  methods: {
    listUser() {
      if (this.user.name === '') {
        this.user.name = null
      }
      if (this.user.address === '') {
        this.user.address = null
      }
      this.$http.get("/inter/user/page", {
        params: {
          name: this.user.name,
          address: this.user.address,
          page: this.current
        }
      }).then(res => {
        this.current = res.current
        this.tableData = res.records
        this.total = res.total
      })
    },

    listUser1(val) {
      this.current = val
      this.listUser()
    },
    handleClick(row) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete("/inter/user/delete?id=" + row.id).then(res => {
          this.$notify({
            type: 'success',
            message: '删除成功!'
          });
          this.current = 1
          this.listUser()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    resetPass(row) {
      this.$confirm('此操作将重置密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete("/inter/user/reset?id=" + row.id).then(res => {
          this.$notify({
            type: 'success',
            message: '重置成功!'
          });
          this.current = 1
          this.listUser()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消重置'
        });
      });
    },
    editUser(row) {
      this.form = row
      this.dialogFormVisible = true
      this.deptOptions = []
      this.roleOptions = []
      this.$http.get("/inter/t-dept/all").then(res => {
        for(let i=0; i< res.length; i++){
          this.deptOptions.push(
          {
            value: res[i].id,
            label: res[i].deptName
          }
        )
        }
       
      })
      this.$http.get("/inter/t-role/list").then(res => {
        for(let i=0; i< res.length; i++){
          this.roleOptions.push(
          {
            value: res[i].id,
            label: res[i].roleName
          }
        )
        }
        
      })
    },
    saveUserEnter(){
      this.$http.post("/inter/user/saveUser",this.form).then(res=>{
        this.dialogFormVisible = false
        this.listUser()
        this.$notify({
          message:"成功",
          type:"success"
        })
      })
    }
  },
};
</script>
  